package Tracker.server.control;

import java.security.NoSuchAlgorithmException;
import java.util.Enumeration;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import Tracker.server.model.TError;
import Tracker.server.model.Track;

public class TrackController {
	ProtocolHandler handler;

	public TrackController(ProtocolHandler h) {
		this.handler = h;
	}

	public void create(JSONObject req, JSONObject res) throws JSONException {
		try {
			JSONObject trackReq = req.getJSONObject("track");
			if (!trackReq.isNull("name") && !trackReq.isNull("location")) {
				Track track = new Track(this.handler.model.getUID(),
						this.handler.session.user, trackReq.getString("name"),
						trackReq.getString("location"));
				
				this.handler.model.addTrack(track);
				res.append("success", new Boolean(true));
				res.append("track", track);
				return;
			} else {
				res.append("success", new Boolean(false));
				res.append(
						"error",
						new TError(
								"Missing fields: track : {name: String, location: String}",
								400));
				return;
			}

		} catch (JSONException e) {
			res.append("success", new Boolean(false));
			res.append("error", new TError("No track object", 400));
			return;
		}
	}
	
	public void get(JSONObject req, JSONObject res) throws JSONException {
		Enumeration<Integer> tracks =  this.handler.model.getTrackIterator();
		res.append("tracks", new JSONArray());
		res.append("success", new Boolean(true));
		
		while(tracks.hasMoreElements()) {
			res.append("tracks", this.handler.model.getTrack(tracks.nextElement()));
		}
		
		return;
	}
	
	public void getImage(JSONObject req, JSONObject res) throws JSONException {
		try {
			JSONObject trackReq = req.getJSONObject("track");

			if (!trackReq.isNull("id")) {
				if (this.handler.model.containsTrack(trackReq.getInt("id"))) {
					Track track = this.handler.model.getTrack(trackReq
							.getInt("id"));

					if (track.owner.id == this.handler.session.user.id) {
						if (!trackReq.isNull("name")) {
							track.setName(trackReq.getString("name"));
						}

						if (!trackReq.isNull("location")) {
							track.setLocation(trackReq.getString("location"));
						}
						
						if(!trackReq.isNull("points")) {
							JSONArray points = trackReq.getJSONArray("points");
							track.put("points", points);
						}

						res.append("success", new Boolean(true));
						res.append("track", track);
						return;
					} else {
						res.append("success", new Boolean(false));
						res.append("error", new TError(
								"Cheese that isn't yours: Nacho Cheese!", 401));
						return;
					}
				} else {
					res.append("success", new Boolean(false));
					res.append("error", new TError("No track id", 400));
					return;
				}
			}

		} catch (JSONException e) {
			res.append("success", new Boolean(false));
			res.append("error", new TError("No track object", 400));
			return;
		}
	}

	public void update(JSONObject req, JSONObject res) throws JSONException,
			NoSuchAlgorithmException {
		try {
			JSONObject trackReq = req.getJSONObject("track");

			if (!trackReq.isNull("id")) {
				if (this.handler.model.containsTrack(trackReq.getInt("id"))) {
					Track track = this.handler.model.getTrack(trackReq
							.getInt("id"));

					if (track.owner.id == this.handler.session.user.id) {
						if (!trackReq.isNull("name")) {
							track.setName(trackReq.getString("name"));
						}

						if (!trackReq.isNull("location")) {
							track.setLocation(trackReq.getString("location"));
						}
						
						if(!trackReq.isNull("points")) {
							JSONArray points = trackReq.getJSONArray("points");
							track.put("points", points);
						}

						res.append("success", new Boolean(true));
						res.append("track", track);
						return;
					} else {
						res.append("success", new Boolean(false));
						res.append("error", new TError(
								"Not Your Track!", 401));
						return;
					}
				} else {
					res.append("success", new Boolean(false));
					res.append("error", new TError("No track id", 400));
					return;
				}
			}

		} catch (JSONException e) {
			res.append("success", new Boolean(false));
			res.append("error", new TError("No track object", 400));
			return;
		}
	}
}
